package leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0225.java
 * @Author : WangRuoyu
 * @Date 2023/11/9 16:54
 */


public class Q0225 {
    int count = 0;
    boolean flag = false;
    Queue<Integer> q0 = new LinkedList<>();
    Queue<Integer> q1 = new LinkedList<>();

    //    public MyStack() {
    public Q0225() {
    }

    public void push(int x) {
        if (flag) {
            q1.add(x);
        } else {
            q0.add(x);
        }
        count++;
    }

    public int pop() {
        int tmp;
        if (flag) {
            for (int i = 0; i < count - 1; i++) {
                q0.add(q1.poll());
            }
            tmp = q1.peek();
            q1.clear();
        } else {
            for (int i = 0; i < count - 1; i++) {
                q1.add(q0.poll());
            }
            tmp = q0.peek();
            q0.clear();
        }
        count--;
        flag = !flag;
        return tmp;
    }

    public int top() {
        int tmp;
        if (flag) {
            for (int i = 0; i < count - 1; i++) {
                q0.add(q1.poll());
            }
            tmp = q1.peek();
            q0.add(q1.poll());
        } else {
            for (int i = 0; i < count - 1; i++) {
                q1.add(q0.poll());
            }
            tmp = q0.peek();
            q1.add(q0.poll());
        }
        flag = !flag;
        return tmp;
    }

    public boolean empty() {
        return count == 0;
    }

    public static void main(String[] args) {
        Q0225 sl = new Q0225();
    }
}